#/bin/bash

html='out.html'
md='out.md'

echo '文档' $1;

# iconv -f gbk -t utf8 $1 -o $md
echo '转化文档到' $md;
cp $1 $md

title=`head -n 1 $md | awk {'print $2'}`;
author=$2

echo '标题' $title;
echo '作者' $2;
echo '输出到HTML';

touch $html;
echo '<html><head><meta http-equiv="content-language" content="zh-CN" /><meta http-equiv="Content-type" content="text/html; charset=utf-8"><meta name="Author" content="' >> $html;
echo $author >> $html;
echo '"><title>' >> $html ;
echo $title >> $html;
echo '</title>' >> $html;
echo '<style>p{text-indent: 2em;}p code{padding:2em;border:1px solid #ccc;font-size: 0.8em;}</style>' >> $html;
echo '</head><body>' >> $html;
~/golang/bin/markdown $md >> $html;
echo '</body></html>' >> $html;

sed -i 's/<img/<img\ style=\"width:100\%\"/g' $html

# ebook-convert out.html $1.mobi --cover cover.png --authors $author --comments 'no comments!' --level1-toc '//h:h1' --level2-toc '//h:h2' --level3-toc '//h:h3' --language 'zh_CN'
echo '输出到 mobi'
ebook-convert out.html $1.mobi --authors $author --comments 'no comments!' --level1-toc '//h:h1' --level2-toc '//h:h2' --language 'zh_CN'


rm -f $html $md;
